//
//  HRZMyOrderViewController.m
//  EMarket
//
//  Created by 石头 on 2018/5/22.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZMyOrderViewController.h"
//  ViewController
#import "UIViewController+AlertViewAndActionSheet.h"
//  model
#import "HRZOrderModel.h"
#import "HRZLocalOrderStatusModel.h"
#import "HRZOrderGoodsDetailModel.h"
//  view
#import "HRZMyOrderEmptyView.h"
#import "HRZMyOrderTitleCell.h"
#import "HRZOrderGoodsDetailCell.h"
#import "HRZOrderFootCell.h"
#import "HRZOrderHeadView.h"
//  other
#import <MJExtension.h>
#import "HRZTool.h"


#define CollectionViewHeight    30

@interface HRZMyOrderViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITableViewDelegate, UITableViewDataSource, HRZOrderFootCellDelegate>

/** 无订单背景view */
@property (nonatomic, strong) HRZMyOrderEmptyView *tableBGView;

@property (nonatomic, strong) NSMutableArray <HRZOrderModel *>*orderArray;

@property (nonatomic, strong) NSMutableArray *titleArray;

@end

static NSString * const HRZMyOrderTitleCellID = @"HRZMyOrderTitleCellID";

static NSString * const HRZOrderHeadViewID = @"HRZOrderHeadViewID";
static NSString * const HRZOrderFootCellID = @"HRZOrderFootCellID";
static NSString * const HRZOrderGoodsDetailCellID = @"HRZOrderGoodsDetailCellID";

@implementation HRZMyOrderViewController

#pragma mark - ----------  lifeCycle ----------
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"我的订单";
    
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.tableBGView];
    
    
    [self setUpUI];
}

- (void)viewWillAppear:(BOOL)animated {
    
}

- (void)viewDidAppear:(BOOL)animated {
    
    [self setUpData];
    
    [self testModels:NO];
}

- (void)setUpUI {
    /** tableView */
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 50;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.frame = CGRectMake(0, CollectionViewHeight, kScreenWidth, kScreenHeight - CollectionViewHeight * 3);
    // 注册cell
    [self.tableView registerClass:[HRZOrderGoodsDetailCell class] forCellReuseIdentifier:HRZOrderGoodsDetailCellID];
    [self.tableView registerClass:[HRZOrderFootCell class] forCellReuseIdentifier:HRZOrderFootCellID];
    // 注册头
    [self.tableView registerClass:[HRZOrderHeadView class] forHeaderFooterViewReuseIdentifier:HRZOrderHeadViewID];
    
    
    /** collectioView */
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollEnabled = NO;
    self.collectionView.frame = CGRectMake(0, 0, kScreenWidth, CollectionViewHeight);
    //  注册cell
    [self.collectionView registerClass:[HRZMyOrderTitleCell class] forCellWithReuseIdentifier:HRZMyOrderTitleCellID];
    
    
    WEAKSELF
    self.tableBGView.goToBuyBlock = ^{
        [weakSelf.tabBarController setSelectedIndex:0];
    };
    
}

- (void)setUpData {
    
    NSMutableArray *titleArr = [NSMutableArray new];
    
    NSArray *array = [HRZTool getLocalFileOrderStatusInfo];
    
    for (int i = 0; i < array.count; i++) {
        HRZLocalOrderStatusModel *item = array[i];
        [titleArr addObject:item.stateTitle];
    }
    
    
    self.titleArray = [[NSMutableArray alloc] initWithArray:titleArr];
    

    NSIndexPath *index = [NSIndexPath indexPathForRow:_status inSection:0];
    [self.collectionView selectItemAtIndexPath:index animated:NO scrollPosition:UICollectionViewScrollPositionNone];
    
    [self.collectionView reloadData];
}

- (void)testModels:(BOOL)isMore {
    if (!isMore) {
        [self.orderArray removeAllObjects];
    }
    
    for (int i = 0; i < arc4random() % 6 + 1; i ++) {
        HRZOrderModel *order = [[HRZOrderModel alloc] init];
        order.orderNumber = [NSString  stringWithFormat:@"%d", arc4random()%10000 + 10000];
        
        NSMutableArray *mutArray = [[NSMutableArray alloc] init];
        for (int j = 0; j < arc4random() % 6 + 1; j++) {
            HRZOrderGoodsDetailModel *goodesModel = [[HRZOrderGoodsDetailModel alloc] init];
            goodesModel.goodsName = [NSString stringWithFormat:@"商品名称编号：%d", arc4random() % 7 * 5];
            goodesModel.goodsPrice = [NSString stringWithFormat:@"%.2f ¥", arc4random() % 78 + arc4random() % 34 * 4.32];
            goodesModel.goodsNumber = [NSString stringWithFormat:@"%d 件", arc4random()%50 + 3];
            [mutArray addObject:goodesModel];
        }
        order.orderDetailDtos = [NSArray arrayWithArray:mutArray];
    
        [self.orderArray addObject:order];
    }
    
    
    [self uiUpdate];
    
    [self stopRereshing];
}

- (void)uiUpdate {
    if (self.orderArray.count == 0) {
        self.tableBGView.hidden = NO;
        self.tableView.hidden = YES;
    } else {
        self.tableBGView.hidden = YES;
        self.tableView.hidden = NO;
    }
    [self.tableView reloadData];
}

#pragma mark - ----  Delegate  ----

#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HRZMyOrderTitleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HRZMyOrderTitleCellID forIndexPath:indexPath];
    cell.titleName = _titleArray[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(kScreenWidth / 5 - 10, CollectionViewHeight);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.orderArray.count == 0) {
        self.tableBGView.hidden = NO;
        self.tableView.hidden = YES;
    } else {
        self.tableBGView.hidden = YES;
        self.tableView.hidden = NO;
    }
    
    [self.tableView.mj_header beginRefreshing];
    
//    [self searchOrdersWithOrderStatus:(HRZOrderStatus)indexPath.item];
}

#pragma mark - <UIColleciontViewDelegate>

#pragma mark - <UITabelViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _orderArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    HRZOrderModel *orderModel = [_orderArray objectAtIndex:section];
    return orderModel.orderDetailDtos.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HRZOrderModel *orderModel = [_orderArray objectAtIndex:indexPath.section];
    UITableViewCell *cell;
    
    if (orderModel.orderDetailDtos.count != indexPath.row) {
        
        HRZOrderGoodsDetailModel *model = orderModel.orderDetailDtos.count ? [orderModel.orderDetailDtos objectAtIndex:indexPath.row] : nil;
        HRZOrderGoodsDetailCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:HRZOrderGoodsDetailCellID];
        goodsCell.model = model;
        
        cell = goodsCell;
    } else {
        HRZOrderFootCell *footCell = [tableView dequeueReusableCellWithIdentifier:HRZOrderFootCellID];
        footCell.delegate = self;
        footCell.model = orderModel;
        cell = footCell;
    }
    return  cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HRZOrderModel *model = self.orderArray[section];
    HRZOrderHeadView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HRZOrderHeadViewID];
    headerView.model = model;
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (void)didSelectdComfirmBtnWithCell:(HRZOrderFootCell *)cell {
    NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
    HRZOrderModel *model = self.orderArray[indexPath.section];
   
    [self AlertWithTitle:@"确认收货" message:[NSString stringWithFormat:@"订单号%@", model.orderNumber] andOthers:@[@"确认", @"取消"] animated:YES action:^(NSInteger index) {
        if (index == 0) {
            [self.orderArray removeObjectAtIndex:indexPath.section];
            [self.tableView reloadData];
        } else {
            
        }
    }];
}

#pragma mark - ----  eventResponse  ----
- (void)searchOrdersWithOrderStatus:(HRZOrderStatus)status {
    WEAKSELF
    [[HRZGlobalNetManager getGlobalNetManager] onSearchOrderByOrderStatus:status complete:^(NSArray<HRZOrderModel *> *result, BOOL isSuc) {
        weakSelf.orderArray = [NSMutableArray arrayWithArray:result];
        [weakSelf.tableView reloadData];
    }];
}

- (void)headerRereshing {
    [self testModels:NO];
}

- (void)footerRereshing {
    [self testModels:YES];
}

- (void)stopRereshing {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)onConfimBtnClickAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark - ----  setting/getting  ----

- (HRZMyOrderEmptyView *)tableBGView{
    if (!_tableBGView) {
        _tableBGView = [[HRZMyOrderEmptyView alloc] initWithFrame:CGRectMake(0, CollectionViewHeight, kScreenWidth, kScreenHeight - CollectionViewHeight)];
    }
    return _tableBGView;
}

- (NSMutableArray<HRZOrderModel *> *)orderArray {
    if (!_orderArray) {
        _orderArray = [[NSMutableArray alloc] init];
    }
    return _orderArray;
}

- (void)setStatus:(HRZOrderStatus)status {
    [self searchOrdersWithOrderStatus:status];
}




@end
